package com.guokai2012.util;

import com.qiniu.util.StringUtils;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

/**
 * @ClassName PropertiesManager
 * @Author guokai2012
 */
public class PropertiesManager {
    private final Properties properties;
    private final String path;
    private String ak;
    private String sk;
    private String bucket;
    private String downUrl;
    private double space;
    private double blobIo;
    private int upStep;

    public PropertiesManager(String path) {
        this.path = path;
        this.properties = new Properties();
        try (FileInputStream in = new FileInputStream(path)) {
            properties.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
        this.ak = properties.getProperty("qiNiuAk");
        this.sk = properties.getProperty("qiNiuSk");
        this.bucket = properties.getProperty("bucket");
        this.downUrl = properties.getProperty("qiNiuDownUrl");
        String blobIo = properties.getProperty("blobIo", "0.0");
        String space = properties.getProperty("space", "0.0");
        String upStep = properties.getProperty("upStep", "0");
        this.blobIo = Double.parseDouble(StringUtils.isNullOrEmpty(blobIo) ? "0.0" : blobIo);
        this.space = Double.parseDouble(StringUtils.isNullOrEmpty(space) ? "0.0" : space);
        this.upStep = Integer.parseInt(StringUtils.isNullOrEmpty(upStep) ? "0" : upStep);
    }

    public void storeProperties() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try (FileOutputStream out = new FileOutputStream(path)) {
            properties.store(out, "设置配置文件内容，在：" + sdf.format(new Date()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String getAk() {
        return ak;
    }

    public void setAk(String ak) {
        this.ak = ak;
        properties.setProperty("qiNiuAk", ak);
    }

    public String getBucket() {
        return bucket;
    }

    public void setBucket(String bucket) {
        this.bucket = bucket;
        properties.setProperty("bucket", bucket);
    }

    public String getDownUrl() {
        return downUrl;
    }

    public void setDownUrl(String downUrl) {
        this.downUrl = downUrl;
        properties.setProperty("qiNiuDownUrl", downUrl);
    }

    public double getBlobIo() {
        return blobIo;
    }

    public void setBlobIo(double blobIo) {
        this.blobIo = blobIo;
        properties.setProperty("blobIo", String.valueOf(blobIo));
    }

    public String getSk() {
        return sk;
    }

    public void setSk(String sk) {
        this.sk = sk;
        properties.setProperty("qiNiuSk", sk);
    }

    public double getSpace() {
        return space;
    }

    public void setSpace(double space) {
        this.space = space;
        properties.setProperty("space", String.valueOf(space));
    }

    public int getUpStep() {
        return upStep;
    }

    public void setUpStep(int upStep) {
        this.upStep = upStep;
        properties.setProperty("upStep", String.valueOf(upStep));
    }
}
